package com.xjh.mange.common.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.xjh.mange.common.constant.IPConst;
import com.xjh.mange.common.constant.WeiXinMsgConst;
import com.xjh.mange.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @auther xjh
 * @create 2021/2/11
 **/
@Slf4j
public class MyUtil {


    /**
     * 发送pwd
     * 失效了
     *
     * @param pwd 松材线虫病
     * @return {@link ResponseVO}
     */
    public static ResponseVO sendPwd(String pwd) {
        String ipAddress = MyIpUtil.getIpAddress(MyRequestUtil.getRequest());
        Map message = new HashMap();
        Map data = new HashMap();
        Map first = new HashMap();
        first.put("value", "登录密码");
        first.put("color", "#173177");
        Map keyword1 = new HashMap();
        keyword1.put("value", "密码为:" + pwd);
        keyword1.put("color", "#173177");
        Map keyword2 = new HashMap();
        Map keyword3 = new HashMap();
        Map keyword4 = new HashMap();
        Map remark = new HashMap();
        keyword2.put("value", DateUtil.format(new Date(), "yyyy-MM-dd HH:ss:mm"));
        keyword2.put("color", "#173177");
        keyword3.put("value", IPConst.address(HttpUtil.get(IPConst.queryIpURL(ipAddress))));
        keyword3.put("color", "#173177");
        keyword4.put("value", ipAddress);
        keyword4.put("color", "#173177");
        remark.put("value", "使用即失效");
        remark.put("color", "#173177");
        message.put("secret", WeiXinMsgConst.SECRET_KEY);
        message.put("app_key", WeiXinMsgConst.APP_KEY);
        message.put("template_id", WeiXinMsgConst.TEMPLATE_KEY);
        message.put("url", null);
        data.put("first", first);
        data.put("keyword1", keyword1);
        data.put("keyword2", keyword2);
        data.put("keyword3", keyword3);
        data.put("keyword4", keyword4);
        data.put("remark", remark);
        message.put("data", data);
        String body = HttpRequest.post(WeiXinMsgConst.URL).header(Header.CONTENT_TYPE, "application/json")
                .body(JSONUtil.toJsonStr(message)).execute().body();
        log.info("消息结果:[{}]",body);
        ResponseVO responseVO = JSONUtil.toBean(JSONUtil.parseObj(body), ResponseVO.class);
        return responseVO;
    }
}
